/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.cef.generator.repository;


import com.inspur.edp.caf.generator.BaseGenerator;
import com.inspur.edp.caf.generator.composite.CompositeGenerator;
import com.inspur.edp.caf.generator.composite.CompositeGeneratorContext;
import com.inspur.edp.cef.designtime.api.IGspCommonDataType;
import com.inspur.edp.cef.entity.config.CefExtendConfig;
import com.inspur.edp.cef.gencommon.config.CefConfigUtils;
import com.inspur.edp.cef.generator.coreGenerator.base.CefCoreGeneratorContext;
import com.inspur.edp.cef.generator.overall.CefGeneratorContext;
import com.inspur.edp.cef.generator.repository.repository.nestedrepository.DataTypeRepositoryGenerator;
import com.inspur.edp.cef.repository.assembler.DataTypeAssembler;
import com.inspur.edp.das.commonmodel.IGspCommonObject;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

public abstract class RepositoryGenerator extends CompositeGenerator {
    private CefCoreGeneratorContext cefContext;
    private IGspCommonDataType gspCommonDataType;
    public RepositoryGenerator(CefCoreGeneratorContext cefContext, IGspCommonDataType gspCommonDataType) {
        this.cefContext = cefContext;
        this.gspCommonDataType = gspCommonDataType;
    }


//ORIGINAL LINE: public new RepositoryGenContext Context => (RepositoryGenContext)super.Context;
    public RepositoryGenContext getRepoGenContext() {
        RepositoryGenContext repositoryGenContext = new RepositoryGenContext();
        repositoryGenContext.setCefContext(cefContext);
        return repositoryGenContext;
    }

    @Override
    protected CompositeGeneratorContext createCompositeContext() {
        RepositoryGenContext context = getGenCtx();
        context.setCefContext(cefContext);
        context.setAssemblerInfo(new RepositoryConfig());
        initAssemblerType(context);
//        intiExtendAssemblerInfo(context);
        return context;
    }

    protected RepositoryGenContext getGenCtx() {
        return new RepositoryGenContext();
    }

    private void initAssemblerType(RepositoryGenContext context) {
//        java.lang.Class assemblerType = getType(cefContext.getCefConfig().getRepositoryConfig());
//        Object tempVar = null;
//        try {
//            tempVar = assemblerType.getConstructor(null).newInstance(null);
//        } catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
//            throw new RuntimeException(assemblerType.getTypeName() + "实例化失败");
//        }
//		assemblerType.Assembly.CreateInstance(assemblerType.FullName);

        //todo wangmj 待验证
//        DataTypeAssembler assemblerInstance = (DataTypeAssembler) ((tempVar instanceof DataTypeAssembler) ? tempVar : null);
//        AssemblerInfo tempVar2 = new AssemblerInfo();
////        tempVar2.setAssemblerType(assemblerType);
////        tempVar2.setAssemblerInstance(assemblerInstance);
//        tempVar2.setAssemblerType(CefRepoAssembler.class);
//        tempVar2.setAssemblerInstance(new CefRepoAssembler(this.gspCommonObject, getRepoGenContext()));
//        context.getAssemblerInfo().setBaseAssembler(tempVar2);
    }

    private void intiExtendAssemblerInfo(RepositoryGenContext context) {
        if (context.getAssemblerInfo().getExtendAssemblers() == null) {
            context.getAssemblerInfo().setExtendAssemblers(new java.util.ArrayList<AssemblerInfo>());
        }

        List<CefExtendConfig> extendConfigs = cefContext.getConfig().getExtendConfigs();
        if (extendConfigs == null || extendConfigs.size() < 1) {
            return;
        }

        //todo wangmj 扩展配置？
        for (CefExtendConfig extendConfig : extendConfigs) {
            if (extendConfig.getRepositoryConfig() != null) {
                java.lang.Class extendType = getType(extendConfig.getRepositoryConfig());
                Object tempVar = null;
                try {
                    tempVar = extendType.getConstructor(null).newInstance(null);
                } catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
                    throw new RuntimeException(extendType.getTypeName() + "实例化失败");
                }

                DataTypeAssembler extendInstance = (DataTypeAssembler) ((tempVar instanceof DataTypeAssembler) ? tempVar : null);
                AssemblerInfo tempVar2 = new AssemblerInfo();
                tempVar2.setAssemblerType(extendType);
                tempVar2.setAssemblerInstance(extendInstance);
                context.getAssemblerInfo().getExtendAssemblers().add(tempVar2);
            }
        }
    }


    private java.lang.Class getType(com.inspur.edp.cef.entity.config.RepositoryConfig config) {
        //todo wangmj
        return null;
//        if (cefContext.getCompileContext() == null) {
//            try {
//                return Class.forName(config.getClassName());
//            } catch (ClassNotFoundException e) {
//                throw new RuntimeException(config.getClassName() + "获取失败", e);
//            }
//        }
//
//        ArrayList<String> jarList = new ArrayList<String>() {
//            {
//                add(cefContext.getCompileContext().getApiJarFileName());
//                add(cefContext.getCompileContext().getCoreJarFileName());
//            }
//        };
//        return CefConfigUtils.getType(jarList, config.getAssembly(), config.getClassName());
//		CefConfigUtils.getType(config.getAssembly(), config.getClass());
    }

//    @Override
//    protected ArrayList<BaseGenerator> createChildGenerators() {
//        java.util.ArrayList<BaseGenerator> gens = new java.util.ArrayList<BaseGenerator>();
//        DataTypeRepositoryGenerator reposGen = getRepositoryGenerator();
//        gens.add(reposGen);
//
//        return gens;
//    }

    @Override
    protected java.util.ArrayList<BaseGenerator> createCompositeItemGenerators() {
        java.util.ArrayList<BaseGenerator> gens = new java.util.ArrayList<BaseGenerator>();
        DataTypeRepositoryGenerator reposGen = getRepositoryGenerator();
        gens.add(reposGen);

        return gens;
    }

    protected abstract DataTypeRepositoryGenerator getRepositoryGenerator();
}
